package com.springcachetest.web;

import com.springcachetest.bean.ParamBean;
import com.springcachetest.bean.Person;
import com.springcachetest.bean.SomeComponent;
import com.springcachetest.service.CacheService;
import com.springcachetest.service.CacheService2;
import com.springcachetest.utils.SpringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 杨志远[of2551]
 *         company qianmi.com
 *         Date    2017-03-14
 */
@RestController
@RequestMapping("/web")
@Slf4j
@Api("springCache相关测试")
public class CacheController {

    @Autowired
    private CacheService2 cacheService2;

    @Autowired
    private CacheService cacheService;

    @ApiOperation("不指定key生成器，单个入参为String类型")
    @RequestMapping(value = "/test/string/{id}",method = RequestMethod.GET )
    public Person testString(@PathVariable Integer id) {
        Person p = cacheService.findByStringId(id+"");
        return p;
    }

    @ApiOperation("不指定key生成器，单个入参为Integer类型")
    @RequestMapping(value = "/test/integer/{id}",method = RequestMethod.GET)
    public Person testInteger(@PathVariable Integer id) {
        Person p = cacheService.findByIntegerId(id);
        return p;
    }

    @ApiOperation("不指定key生成器，有多个入参")
    @RequestMapping(value = "/test/mutil/{id}",method = RequestMethod.GET)
    public Person testMutil(@PathVariable Integer id) {
        Person p = cacheService.findByMutilParam(id+"","TwoParam");
        return p;
    }

    @ApiOperation("不指定key生成器，有单个入参，入参为自定义类型")
    @RequestMapping(value = "/test/custom/{id}",method = RequestMethod.GET)
    public Person testCustom(@PathVariable Integer id) {
        ParamBean paramBean = new ParamBean();
        paramBean.setId(id);
        paramBean.setName("名称");
        paramBean.setNum(0);

        Person p = cacheService.findByCustomBean(paramBean);
        return p;
    }

    @ApiOperation("不指定key生成器，有单个入参，入参为基本类型")
    @RequestMapping(value = "/test/basic/{id}",method = RequestMethod.GET)
    public Person testBasic(@PathVariable int id) {
        Person p = cacheService.findByIntId(id);
        return p;
    }

    @ApiOperation("不指定key生成器，有单个入参，入参为基本类型")
    @RequestMapping(value = "/test/genekey/{id}",method = RequestMethod.GET)
    public Person testGeneratorKey(@PathVariable int id) {
        Person p = cacheService.findByIntIdKeyGenerator(id);
        return p;
    }

    @ApiOperation("清除key，key为id")
    @RequestMapping(value = "/test/evict/{id}",method = RequestMethod.GET)
    public String testEvictById(@PathVariable String id) {
      cacheService.evictById(id);
      return "清除成功,id="+id;
    }

    @ApiOperation("更新key，key为id")
    @RequestMapping(value = "/test/update/{id}",method = RequestMethod.GET)
    public String testupdateCacheById(@PathVariable String id) {
      cacheService.updateCacheById(id);
      return "更新成功,id="+id;
    }



    @ApiOperation("调用CacheService2,CacheService2统一配置SpringCache信息")
    @RequestMapping(value = "/test/service2/{id}",method = RequestMethod.GET)
    public Person test14(@PathVariable String id) {
        Person p = cacheService2.findByStringId(id);
        return p;
    }



    @ApiOperation("缓存命中成功")
    @RequestMapping(value = "/hit/success/{id}",method = RequestMethod.GET)
    public Person test11(@PathVariable Integer id) {
        Person p = cacheService.findByIdDirect(String.valueOf(id));
        return p;
    }


    @ApiOperation("缓存命中失败")
    @RequestMapping(value = "/hit/fail/{id}",method = RequestMethod.GET)
    public Person test12(@PathVariable Integer id) {
        Person p = cacheService.findByIdIndirect(String.valueOf(id));
        return p;
    }

    @ApiOperation("cache condition test")
    @RequestMapping(value = "/hit/{number}",method = RequestMethod.GET)
    public Person test13(@PathVariable Integer number) {
        Person p = cacheService.testCondition(number==1? SomeComponent.builder().flag(true).build():SomeComponent.builder().flag(false).build());
        return p;
    }



   /* @RequestMapping(value = "/ehcache/get/{id}",method = RequestMethod.GET)
    public Person test13(@PathVariable Integer id) {
//         cacheManager = CacheManager
        EhCacheCacheManager ehcache = (EhCacheCacheManager)SpringUtil.getBean("ehcache");


    }*/





}
